package com.glsc.ngateway.platform.service.system;

import com.glsc.ngateway.common.base.domain.mysql.gateway.system.SystemVendorLogInfo;
import com.glsc.ngateway.common.api.platform.dto.system.ParamSystemVendorLogInfoDto;
import com.glsc.ngateway.common.base.exception.PlatformException;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItApplicationDetailRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItOperationRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItVirtualDeviceRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.system.SystemVendorInfoRepository;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.system.SystemVendorLogInfoRepository;
import com.glsc.ngateway.platform.service.MailService;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.platform.service.it.SequenceService;
import com.glsc.ngateway.platform.service.it.ServerService;
import com.glsc.ngateway.common.api.common.enums.DictConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Optional;

@Service
public class SystemVendorLogInfoService {

    Logger logger = LoggerFactory.getLogger(SystemVendorLogInfoService.class);
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private UserService userService;
    @Autowired
    private ItVirtualDeviceRepo itVirtualDeviceRepo;
    @Autowired
    private ItOperationRepo itOperationRepo;
    @Autowired
    private ItApplicationDetailRepo itApplicationDetailRepo;

    @Autowired
    private ServerService serverService;

    @Autowired
    private MailService mailService;

    @Resource
    private SystemVendorInfoRepository systemVendorInfoRepository;
    @Resource
    private SystemVendorLogInfoRepository systemVendorLogInfoRepository;

    @Transactional
    public void addOrUpdate(SystemVendorLogInfo bean) {
        bean.setDeleted(DictConstant.NO_DELETE);
        systemVendorLogInfoRepository.save(bean);
    }

    public void delete(Integer id) {
        SystemVendorLogInfo bean = null;
        Optional<SystemVendorLogInfo> optional = systemVendorLogInfoRepository.findById(id);
        if (optional.isPresent()) {
            bean = optional.get();
        } else {
            throw PlatformException.errorWithOutMail("数据不存在");
        }
        bean.setDeleted(DictConstant.DELETED);
        systemVendorLogInfoRepository.save(bean);
    }

    public Page<SystemVendorLogInfo> search(ParamSystemVendorLogInfoDto params) {
        params.setDeleted(DictConstant.NO_DELETE);
        Pageable pageable = PageRequest.of(params.getPageNo()- 1, params.getPageSize());
        Page<SystemVendorLogInfo> result = systemVendorLogInfoRepository.searchByParam(pageable,params);
        return  result;
    }

}
